package com.linkface.utils;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import java.util.List;

/**
 * Copyright (c) 2017-2018 LINKFACE Corporation. All rights reserved.
 */
public class LFSensorAccelerometerController implements SensorEventListener {
    public static final String TAG = "LFSensorAccelerometerController";

    public static final int WAIT_DURATION = 500;

    public static final int STATUS_NONE = 0;
    public static final int STATUS_STATIC = 1;
    public static final int STATUS_MOVE = 2;

    private SensorManager mSensorManager;
    private Sensor mSensor;

    private float mX, mY, mZ;
    private long mLastStaticStamp = 0;

    private int mCurrentStatus = STATUS_NONE;

    private CameraFocusListener mCameraFocusListener;

    private static LFSensorAccelerometerController mInstance;

    private LFSensorAccelerometerController(Context context) {
        mSensorManager = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// TYPE_GRAVITY
    }

    public static LFSensorAccelerometerController getInstance(Context context) {
        if (mInstance == null) {
            synchronized (LFSensorAccelerometerController.class) {
                if (mInstance == null) {
                    mInstance = new LFSensorAccelerometerController(context);
                }
            }
        }
        return mInstance;
    }

    public void setCameraFocusListener(CameraFocusListener mCameraFocusListener) {
        this.mCameraFocusListener = mCameraFocusListener;
    }

    public boolean isSupportAccelerometerSensor() {
        boolean isSupport = true;
        if (mSensorManager != null) {
            List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
            isSupport = sensorList != null && sensorList.size() >= 1;
        }
        return isSupport;
    }

    public void onStart() {
        mSensorManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onStop() {
        mSensorManager.unregisterListener(this, mSensor);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == null) {
            return;
        }

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            long stamp = System.currentTimeMillis();
            LFLog.i(TAG, "TYPE_ACCELEROMETER", "x", x);
            LFLog.i(TAG, "TYPE_ACCELEROMETER", "y", y);
            LFLog.i(TAG, "TYPE_ACCELEROMETER", "z", z);

            if (mCurrentStatus != STATUS_NONE) {
                float px = Math.abs(mX - x);
                float py = Math.abs(mY - y);
                float pz = Math.abs(mZ - z);
                double value = Math.sqrt(px * px + py * py + pz * pz);
                LFLog.i(TAG, "onSensorChanged", "px", px);
                LFLog.i(TAG, "onSensorChanged", "py", py);
                LFLog.i(TAG, "onSensorChanged", "pz", pz);
                LFLog.i(TAG, "onSensorChanged", "value", value);
                if (value >= 0.6) {
                    mCurrentStatus = STATUS_MOVE;
                } else {
                    //上一次状态是move，记录静态时间点
                    if (mCurrentStatus == STATUS_MOVE) {
                        LFLog.i(TAG, "mCameraFocusListener", "onFocus");
                        if (mCameraFocusListener != null) {
                            mCameraFocusListener.onFocus();
                        }
                        mLastStaticStamp = stamp;
                    }

                    mCurrentStatus = STATUS_STATIC;
                }
            } else {
                mLastStaticStamp = stamp;
                mCurrentStatus = STATUS_STATIC;
            }

            mX = x;
            mY = y;
            mZ = z;
        }
    }

    public interface CameraFocusListener {
        void onFocus();
    }
}
